package de.acagamics.roteschlupfmuetze.core;

import java.util.LinkedList;

import java.awt.Graphics2D;
import java.awt.image.BufferedImage;

import com.golden.gamedev.object.PlayField;
import com.golden.gamedev.object.Sprite;
import com.golden.gamedev.object.SpriteGroup;
import com.golden.gamedev.object.background.ImageBackground;

import de.acagamics.roteschlupfmuetze.RoteSchlupfmuetze;
import de.acagamics.roteschlupfmuetze.data.LevelData;
import de.acagamics.roteschlupfmuetze.level.*;

public class Render {
	public static Render render;
	
	BufferedImage tile = RoteSchlupfmuetze.loadImage(
			"de/acagamics/roteschlupfmuetze/dummy/tile_sample.png");
	
	PlayField field = new PlayField(new ImageBackground(RoteSchlupfmuetze.loadImage(
    			"de/acagamics/roteschlupfmuetze/dummy/bg_sample.png")
    		));
	
	Sprite loadScreen = new Sprite(RoteSchlupfmuetze.loadImage(
			"de/acagamics/roteschlupfmuetze/res/fading_black.png"));
	
	Cloud[] clouds = new Cloud[5];
	
	SpriteGroup BACKGROUND = new SpriteGroup("Background");
	SpriteGroup STATIC_TILES = new SpriteGroup("Static Tiles");
	SpriteGroup MOVABLE_TILES = new SpriteGroup("Movable Tiles");
	SpriteGroup COLLISION_TILES = new SpriteGroup("Collision Tiles");
	SpriteGroup LEVEL = new SpriteGroup("Level"); // includes Objects // TODO: include Objects
	SpriteGroup PLAYER;
	SpriteGroup OBJECTS = new SpriteGroup("Objects");
	
	SpriteGroup GUI = new SpriteGroup("GUI");
	
	PlayerTileCollision collision = new PlayerTileCollision();
	GridGenerator gridGenerator = new GridGenerator();
	
	
	public Render(SpriteGroup playerSprites) {
		Render.render = this;
		PLAYER = playerSprites;
		field.addGroup(BACKGROUND);
		field.addGroup(PLAYER);
		field.addGroup(STATIC_TILES);
		field.addGroup(MOVABLE_TILES);
		field.addGroup(COLLISION_TILES);
		initGUI();
		field.addGroup(GUI);
		field.addGroup(OBJECTS);
		field.addCollisionGroup(PLAYER, COLLISION_TILES, collision);
	}
	
	private void initGUI() {
		GUI.add(new Sprite(RoteSchlupfmuetze.loadImage(
				"de/acagamics/roteschlupfmuetze/dummy/inventory_sample.png")));
		GUI.add(new Sprite(RoteSchlupfmuetze.loadImage(
				"de/acagamics/roteschlupfmuetze/dummy/vignette.png")));
		for(int i=0; i < clouds.length; i++) {
			BACKGROUND.add((clouds[i] = new Cloud(i, 5)).getSprite());
		}
		BACKGROUND.add(loadScreen);
	}
	
	public void reset() {
		for(Sprite sprite : LEVEL.getSprites()) {
			if (sprite != null)
				sprite.setActive(false);
		}
		STATIC_TILES.reset();
		MOVABLE_TILES.reset();
		COLLISION_TILES.reset();
		LEVEL.reset();
		loadScreen.forceX(0);
		loadScreen.forceY(0);
	}
	
	public void removeFromCollision(Sprite sprite) {
		COLLISION_TILES.remove(sprite);
	}
	
	public void loadLevel(LevelData levelData) {
		// grid
		gridGenerator.generateWithSize(levelData.getPitSizeX(), levelData.getPitSizeY(),
				levelData, STATIC_TILES, MOVABLE_TILES, COLLISION_TILES, LEVEL);
		// objects
		LinkedList<LevelObject> objects = levelData.getObjects();
		for(LevelObject object : objects) {
			OBJECTS.add(object.getObjectSprite());
			LEVEL.add(object.getObjectSprite());
		}
		loadScreen.addHorizontalSpeed(1, 1.2, 50);
	}
	
	public void move() {
		RoteSchlupfmuetze.moveGroup(MOVABLE_TILES, 10, 0.08, 0.45);
	}
	
	public void update(long elapsedTime) {
		for(Cloud cloud : clouds) {
			cloud.update(elapsedTime);
		}
		field.update(elapsedTime);
	}
	
	public void render(Graphics2D g) {
		field.render(g);
	}
}
